<template>
  <div>
    <div class="flex mb-3 flex-nowrap">
      <div class="w-56">
        <el-input placeholder="请输入手机号" v-model="state.search">
          <template #append>
            <el-button type="primary" @click="handleSearch"> 搜索 </el-button>
          </template>
        </el-input>
      </div>
      <div class="flex-1 text-right">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click.prevent="add"
        >
          添加
        </el-button>
      </div>
    </div>
    <div class="mb-3">
      <el-table
        :data="tableData"
        height="calc(100vh - 14rem)"
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column
          prop="nickname"
          label="昵称"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          label="手机号"
          width="160"
        ></el-table-column>
        <el-table-column prop="role" label="角色" width="120"></el-table-column>
        <el-table-column prop="remark" label="备注">
          <template #default="scope">
            <div class="truncate">{{ scope.row.remark }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="addtime"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <div>
              <el-tag v-if="scope.row.status == 1" type="success">启用</el-tag>
              <el-tag v-if="scope.row.status == 0" type="warning">停用</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作" width="140">
          <template #default="scope">
            <el-button type="text" @click.prevent="() => setState(scope.row)">
              {{ scope.row.status == 1 ? "停用" : "启用" }}
            </el-button>
            <el-button type="text" @click.prevent="() => edit(scope.row)">
              编辑
            </el-button>
            <el-button type="text" @click.prevent="() => del(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      @current-change="handleCurrentChange"
      :current-page="state.current_page"
      layout="total, prev, pager, next, jumper"
      :page-size="state.page_size"
      :total="state.total"
    >
    </el-pagination>
    <el-dialog
      :title="state.title"
      v-model="state.dialogFormVisible"
      width="700px"
    >
      <el-form :model="state.form" :rules="rules" ref="ruleForm">
        <el-form-item label="手机号：" label-width="100px" prop="mobile">
          <el-input v-model="state.form.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称：" label-width="100px" prop="nickname">
          <el-input v-model="state.form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色：" label-width="100px" prop="role">
          <el-select
            v-model="state.form.role"
            class="w-full"
            placeholder="请选择"
          >
            <el-option
              v-for="item in state.roles"
              :key="item.id"
              :label="item.role"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注：" label-width="100px" prop="remark">
          <el-input v-model="state.form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="setVisible">取 消</el-button>
          <el-button type="primary" @click="submitForm"> 确 定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
const ruleForm = ref(null);
const state = reactive({
  title: "添加账号",
  dialogFormVisible: false,
  form: {},
  roles: [
    {
      id: 1,
      role: "运营",
    },
    {
      id: 2,
      role: "测试",
    },
  ],
  search: "",

  total: 0,
  page_size: 20,
  current_page: 1,
});

const rules = {
  mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
  nickname: [
    { required: true, message: "请输入昵称", trigger: "blur" },
    { max: 20, message: "长度限制为20字符", trigger: "blur" },
  ],
  role: [{ required: true, message: "请选择角色", trigger: "change" }],
  remark: [{ max: 40, message: "长度限制为40字符", trigger: "blur" }],
};

const tableData = [];
for (let i = 1; i <= 20; i++) {
  tableData.push({
    id: i,
    nickname: "张德江",
    mobile: "13953568545",
    role: 1,
    remark: "运营专用",
    addtime: "2021-09-04 14:22:30",
    status: i % 2,
  });
}

state.total = tableData.length;
function setVisible() {
  state.dialogFormVisible = !state.dialogFormVisible;
}
function handleSearch() {
  console.log("搜索", state.search);
}
function add() {
  state.title = "添加账号";
  setVisible();
}
function setState() {
  console.log("启用/停用");
}
function edit(row) {
  state.title = "编辑账号";
  setVisible();
}

function del(row) {
  ElMessageBox.confirm("确定删除该账号？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      ElMessage.success({
        message: "删除成功!",
      });
    })
    .catch(() => {
      ElMessage.info({
        message: "已取消删除",
      });
    });
}
function handleDel() {}
function handleCurrentChange(current_page) {
  state.current_page = current_page;
}

function submitForm() {
  ruleForm.value.validate((valid) => {
    if (valid) {
      console.log(state.form);
      alert("submit!");
    } else {
      console.log("error submit!!");
      return false;
    }
  });
}
</script>
<style>
.el-input-group__append .el-button.el-button--primary.el-button--small {
  background-color: #409eff;
  color: #ffffff;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}
</style>
